/**
 * 玩家（这里指 洛克人 ）水平运动
 */
//改变方向枚举
var HowToChangeDirection = {
    WhenPressTheCorrespondingInputs: 1,//玩家自己输入
}

//玩家水平移动的方向
var DirectionsXAxisEnum = {
    LEFT: -1,
    RIGHT: 1,
}

var currentDirection = DirectionsXAxisEnum.LEFT////玩家当前面向的方向?

//玩家在水平中移动的速度
var velocity = 1;


//玩家停下每毫秒降低的速度
var constantReductionOfVelocity = false;//是否开始降低速度
var speedToReduceVelocity = 0.5;

//逐渐增加速度以达到速度值
var gradualVelocity = false;
var speedToIncreaseVelocity = 0;

var startToReduceVelocityInX = false;

//速度方向
var constantVelocityRight = false;
var constantVelocityLeft = false;

//玩家是否蹲下
var playerCanCrouchDown = false;


//Keys to move
var canMoveToRight = false;
var inputControlToRight = GameEvent.KEY.RIGHT;

var canMoveToLeft = false;
var inputControlToLeft = GameEvent.KEY.LEFT;


cc.Class({
    extends: cc.Component,

    properties: {
        player: cc.Node,
        main: cc.Node,
    },

    changeVelocityInX: function (value) {
        if (this.player && value != 0) {
            var com = this.player.getComponent("player");
            this.player.scaleX = currentDirection;
            com.playAn(PlayerStates.PlayerAnimaEnemu.RUN)
        }
    },




    lateUpdate: function () {
        if (startToReduceVelocityInX) {
            startToReduceVelocityInX = false;
            if (this.player) {
                var com = this.player.getComponent("player");
                com.stopAn(PlayerStates.PlayerAnimaEnemu.RUN)
                com.playAn(PlayerStates.PlayerAnimaEnemu.STAND)
                
            }
        }
    },


    movementLeftStart: function () {

        currentDirection = DirectionsXAxisEnum.LEFT;
        // if (!PlayerStates.is(PlayerStatesEnum.PlayerStartedToMove))
        //     PlayerStates.setPlayerState(PlayerStatesEnum.PlayerStartedToMove, true);


        startToReduceVelocityInX = false;
        //Call the function to move
        this.changeVelocityInX(velocity);
    },

    movementRightStart: function () {

        currentDirection = DirectionsXAxisEnum.RIGHT;

        // if (!PlayerStates.is(PlayerStatesEnum.PlayerStartedToMove))
        //     PlayerStates.setPlayerState(PlayerStatesEnum.PlayerStartedToMove, true);


        startToReduceVelocityInX = false;
        //Call the function to move
        this.changeVelocityInX(velocity);
    },

    movementStop: function () {
        startToReduceVelocityInX = true;
    },

    controlsMovementLeft: function () {

        //If press the button
        if (InputControl.getControl(inputControlToLeft) && !InputControl.getControl(inputControlToRight)) {
            this.movementLeftStart();
        }
    },
    controlsMovementRight: function () {

        //If press the button
        if (InputControl.getControl(inputControlToRight) && !InputControl.getControl(inputControlToLeft)) {
            this.movementRightStart();
        }

    },

    checkStop: function () {
        if (!InputControl.getControl(inputControlToRight) && !InputControl.getControl(inputControlToLeft) && PlayerStates.is(PlayerStates.PlayerStatesEnum.PlayerStartedToMove)) {
            // this.movementStop();
            PlayerStates.setPlayerState(PlayerStates.PlayerStatesEnum.PlayerStand)
            cc.log("停止了")
        }
    },

    CrouchDownPCControls: function () {

    },

    update:function(){
        this.checkStop();
        this.controlsMovementLeft();
        this.controlsMovementRight();
    },

    

    onPlayerEvent: function (data) {
        cc.log("onPlayerEvent data",data)
        if (data.state === PlayerStates.PlayerStatesEnum.PlayerStand) {
            this.movementStop();
            this.player.getComponent("player").setGround(true)
        }
    },

    ctor: function () {
        // this._refrestTime = 0.1
        // this.addTime = 0;
    },

    onLoad: function () {

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        GameEvent.addCmd(GameEvent.PLAYER_EVENT, this.onPlayerEvent, this)
    },

    // update (dt) {},
});
